package com.whl.basic.controller;

import com.whl.basic.dto.SmsCodeDto;
import com.whl.basic.exception.BusinessException;
import com.whl.basic.service.IVerifyCodeService;
import com.whl.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 接口类：验证码
 * 图形验证码
 * 手机验证码
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 获取图形验证码
     * @param key
     * @return
     */
    @GetMapping("/image/{key}")
    public AjaxResult imageCode(@PathVariable String key){
        try {
            String imageCodeStr = verifyCodeService.imageCode(key);
            return AjaxResult.me().setResultObj(imageCodeStr);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取图形验证码失败");
        }
    }

    /**
     * 发送手机验证码
     * @param smsCodeDto
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult sendSmsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyCodeService.sendSmsCode(smsCodeDto);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取图形验证码失败");
        }
    }

    /**
     * 发送微信验证码
     * @param smsCodeDto
     * @return
     */
    @PostMapping("/binderSmsCode")
    public AjaxResult sendBinderSmsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyCodeService.sendBinderSmsCode(smsCodeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取图形验证码失败");
        }
    }

}
